<template>
  <div>
    <div class="flex justify-between items-center mb-2">
      <div class="absolute top-[-9999px] hidden">
        <a-button ref="printLong" v-print="{ id: 'printLong' }">打印</a-button>
        <a-button ref="printSort" v-print="{ id: 'printSort' }">打印</a-button>
        <a-button ref="printBen" v-print="{ id: 'printBen' }">打印</a-button>
        <!-- 长期医嘱 -->
        <PrintViewLong id="printLong" :print-data="printData" />
        <!-- 临时医嘱 -->
        <PrintViewShort id="printSort" :print-data="printData" />
      </div>
      <div class="ml-6">
        <a-radio-group v-model:value="store.allAdvType" @change="onChangeType">
          <a-radio value="1">本次签收</a-radio>
          <a-radio value="2">全部</a-radio>
        </a-radio-group>
      </div>
      <div class="flex">
        <a-button @click="onPrintAdv(1)"><BaseIcon name="PrinterOutlined" :loading="printLoading1" />长期医嘱单</a-button>
        <a-button class="ml-2" @click="onPrintAdv(0)"><BaseIcon name="PrinterOutlined" :loading="printLoading2" />临时医嘱单</a-button>
      </div>
    </div>
    <BaseTable
      fill
      column-code="00001110"
      :loading="!!store.loading"
      :data-source="store.allOrderData"
      :row-selection="{
        selectedRowKeys,
        getCheckboxProps: (record) => ({
          disabled: record.isChid
        }),
        onChange: onSelectChange
      }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'advStatus'">
          <a-tag color="blue">{{ record.advStatusName || '-' }}</a-tag>
        </template>
        <template v-if="column.dataIndex === 'advExecType'">
          <!-- 医嘱执行类别;0：临时 1：长期 2：出院带药 -->
          <span v-if="record.advExecType == '0'">临时</span>
          <span v-if="record.advExecType == '1'">长期</span>
          <span v-if="record.advExecType == '2'">出院带药</span>
        </template>
        <template v-if="column.dataIndex === 'children'">
          <template v-if="record.advOrderType === 0">
            <DoctorAdviceOrder
              :row-data="record"
              :params-key="{
                mc: 'adv.mc',
                gg: 'adv.gg',
                sl: 'clinicalDose',
                dw: 'clinicalDoseUnit',
                yf: 'deliverRoute.fyff',
                pl: 'fqy.plid',
                drugPathName: 'drugPathName'
              }"
            >
              <!-- <template v-if="record.drugPathName === '是'" #suffix>
                <a-button type="primary" shape="circle" size="small" class="bg-orange-400">备</a-button>
              </template> -->
            </DoctorAdviceOrder>
          </template>
          <template v-else>
            <div class="pl-2">
              <!-- {{ record?.advName }} -->
              <a-tooltip>
                <template #title>{{ record?.advName }}</template>
                {{ record?.advName }}
              </a-tooltip>
            </div>
          </template>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-button type="link" size="small">费用查询</a-button>
          <a-divider type="vertical" />
          <a-button type="link" size="small">发药查询</a-button>
        </template>
      </template>
    </BaseTable>
    <ExpenseQuery v-model:open="expenseQueryOpen" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import { getSystemConfig } from '@/utils/systemConfig'
import useStore from '../store'
import ExpenseQuery from './ExpenseQuery'
import PrintViewLong from './print/PrintViewLong'
import PrintViewShort from './print/PrintViewShort'

const expenseQueryOpen = ref(false)
const store = useStore()
const selectedRowKeys = ref([])
const selectedRows = ref([])
const printData = ref({})
const printLong = ref()
const printSort = ref()
const printBen = ref()

// const onPageChange = ({ current, pageSize }) => {
//   store.fetchAllOrderData(current, pageSize)
// }
watch(
  () => store.allOrderData,
  () => {
    selectedRowKeys.value = []
    selectedRows.value = []
  },
  { immediate: true }
)
const onSelectChange = (keys, rows) => {
  selectedRowKeys.value = keys
  selectedRows.value = rows
}
const onChangeType = () => {
  store.fetchAllOrderData()
  store.fetchSignCountData()
  store.fetchAllOrderCountData()
}
const printLoading1 = ref(false)
const printLoading2 = ref(false)
const onPrintAdv = async (type) => {
  //type 0 : 临时医嘱单 1:长期医嘱单
  if (selectedRows.value.length <= 0) return message.warning('请先勾选医嘱单')
  const clinicalOrderListList = selectedRows.value.filter((item) => item.advExecType === type)
  if (!clinicalOrderListList || clinicalOrderListList.length === 0) {
    if (type === 0) return message.warning('请勾选临时医嘱单')
    if (type === 1) return message.warning('请勾选长期医嘱单')
  }
  printLoading1.value = type === 1
  printLoading2.value = type === 0
  const { code, msg, data } = await post('/inp/Print/printTemporaryDoctorOrders', { clinicalOrderListList })
  printLoading1.value = false
  printLoading2.value = false
  if (code !== 0) return message.error(msg)
  printData.value = JSON.parse(data)

  if (type === 1) {
    if (getSystemConfig('NAVYHIS_MODE') === '1') {
      printData.value = JSON.parse(data)
      setTimeout(() => {
        printLong.value.$el.click()
      }, 1000)
    } else {
      window.openExe?.rpt(data) //调用winform打印
    }
  } else if (type === 0) {
    if (getSystemConfig('NAVYHIS_MODE') === '1') {
      printData.value = JSON.parse(data)
      setTimeout(() => {
        printSort.value.$el.click()
      }, 1000)
    } else {
      window.openExe?.rpt(data) //调用winform打印
    }
  }
}
// const printType = ref('1')
// const onPrintBen = async (e) => {
//   if (selectedRows.value.length <= 0) return message.warning('请先勾选医嘱单')
//   const { code, msg, data } = await post('/inp/Print/printOralBenDoctorOrders', {
//     clinicalOrderListList: selectedRows.value,
//     reportType: e.item.value,
//     bqbh: store.searchState.bqid,
//     inpAdvice: store.searchState.inpAdvice
//   })
//   if (code !== 0) return message.error(msg)
//   if (getSystemConfig('NAVYHIS_MODE') === '1') {
//     printType.value = e.item.value
//     printData.value = JSON.parse(data)
//     if (!printData.value.printData.detail?.length) return message.error('没有需要打印的数据')
//     setTimeout(() => {
//       printBen.value.$el.click()
//     }, 1000)
//   } else {
//     window.openExe?.rpt(data) //调用winform打印
//   }
// }
</script>
